import axios from "axios";
import { nanoid } from "nanoid";
const personOptions = {
    //开启命名空间
    namespaced: true,    
    actions:{
        addPersonWang(context, person) {    
            if(person.name.indexOf('王') === 0) {    
                context.commit('addPerson', person)    
            } else  {
                alert('姓名必须以王开头')
            }
        },
        addPersonServer(context) {
            axios.get('https://api.uixsj.cn/hitokoto/get?type=social').then(response => {    
                context.commit('addPerson', {id:nanoid(),name:response.data})      
            }).catch(error => {      
                alert(error.msg);
                console.log(error)      
            });
        }

    },
    mutations:{
        //添加人员
        addPerson(state, person) {
            console.log('mutations addPerson 执行了');
            state.personList.unshift(person);
        }
    },
    state: {
        personList:[
            {id:1,name:'张三'}
        ]
    },
    getters: {
        firstPersonName(state) {
            return state.personList[0].name;
        }
    }
}

export default personOptions;